package club.xinghe.servlet.teacher;

import club.xinghe.util.JsonServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/teacher/addExamServlet_action")
public class addExamServlet_action extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int tid = Integer.parseInt(request.getParameter("tid"));
        String estring = request.getParameter("estring");
        int tdy = Integer.parseInt(request.getParameter("tdy"));
        List<String> list = new ArrayList<>();
        Map map = new HashMap();

        String[] es1 = null;
        if (estring.length() == 1) {
            es1 = new String[]{estring};
            for (int i = 0; i < es1.length ; i++) {
                list.add(es1[i]);
            }
        } else if (estring.length() > 1) {
            es1 = estring.split(",");
            for (int i = 0; i < es1.length ; i++) {
                list.add(es1[i]);
            }
        }
        if (tdy==0){
            int n = 0;
            for (int i = 0; i < list.size() ; i++) {
                if (Integer.parseInt(list.get(i))==tid){
                    n++;
                }
            }
            if (n==0){
                list.add(String.valueOf(tid));
            }
        }
        if (tid==0){
            list.remove(String.valueOf(tdy));
        }
        String estring1 = "";
        if (list.size()>0){
            for (String tt:list
            ) {
                estring1 += tt +",";
            }
            JsonServlet.getJson(request,response,estring1.substring(0,estring1.length()-1));
        }
        if (list.size()==0){
            JsonServlet.getJson(request,response,estring1);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
